﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
namespace Business
{
    /// <summary>
    /// 主要逻辑
    /// </summary>
    public class MainMange
    {
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="str_task_decript">任务内容</param>
        /// <param name="str_task_time">任务时间</param>
        /// <param name="str_task_type">任务类型</param>
        /// <param name="str_task_state">任务状态</param>
        /// <param name="str_task_contact">任务联系人</param>
        /// <param name="str_task_refrences_user_email"></param>
        /// <returns>正确添加返回id,错误返回null</returns>
        public static string add_task(String str_task_decript, String str_task_time, String str_task_type, String str_task_state, String str_task_contact, String str_task_refrences_user_email)
        {
            task tk = new task();
            if (str_task_time != "")
            {
                tk.task_time = str_task_time;
            }
            tk.task_decript = str_task_decript;
            tk.task_type = str_task_type;
            tk.task_state = str_task_state;
            tk.task_contact = str_task_contact;
            tk.task_refrences_user_email = str_task_refrences_user_email;
            BLL_task bll_tk = new BLL_task();
            if (bll_tk.insert(tk))
            {
                string id = FindID_task(str_task_decript, str_task_time, str_task_type, str_task_state, str_task_contact, str_task_refrences_user_email);
                return id;
            }
            return null;
        }

        /// <summary>
        /// 查找任务ID
        /// </summary>
        /// <param name="str_task_decript"></param>
        /// <param name="str_task_time"></param>
        /// <param name="str_task_type"></param>
        /// <param name="str_task_state"></param>
        /// <param name="str_task_contact"></param>
        /// <param name="str_task_refrences_user_email"></param>
        /// <returns>正确返回ID，错误返回null</returns>
        public static string FindID_task(String str_task_decript, String str_task_time, String str_task_type, String str_task_state, String str_task_contact, String str_task_refrences_user_email)
        {
            task tk = new task();
            tk.task_decript = str_task_decript;
            tk.task_time = str_task_time;
            tk.task_type = str_task_type;
            tk.task_state = str_task_state;
            tk.task_contact = str_task_contact;
            tk.task_refrences_user_email = str_task_refrences_user_email;
            BLL_task bll_tk = new BLL_task();
            List<task> listtk = bll_tk.search(tk, "all");
            if (listtk != null)
            {
                if (listtk.Count > 0)
                {
                    return "" + listtk[0].task_id;
                }
            }
            return null;
        }

        /// <summary>
        /// 将任务标志为完成
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns>成功返回true</returns>
        public static bool finish_task(String taskid)
        {
            task tk = new task();
            string str = taskid;
            tk.task_id = Decimal.Parse(str);
            BLL_task bll_tk = new BLL_task();
            List<task> listtk = bll_tk.search(tk, "task_id");
            if (listtk != null)
            {
                if (listtk.Count > 0)
                {
                    listtk[0].task_state = DefaultDB.DF_TASKSTATE_FINISH;
                    if (bll_tk.update(listtk[0]))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns>成功返回true</returns>
        public static bool delete_task(String taskid)
        {
            task tk = new task();
            string str = taskid;
            tk.task_id = Decimal.Parse(str);
            BLL_task bll_tk = new BLL_task();
            if (bll_tk.delete(tk))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改任务
        /// </summary>
        /// <param name="taskid"></param>
        /// <param name="str_task_decript"></param>
        /// <param name="str_task_time"></param>
        /// <param name="str_task_type"></param>
        /// <param name="str_task_contact"></param>
        /// <returns>成功返回true</returns>
        public static bool change_task(String taskid, String str_task_decript, String str_task_time, String str_task_type, String str_task_contact)
        {
            task tk = new task();
            string str = taskid;
            tk.task_id = Decimal.Parse(str);
            BLL_task bll_tk = new BLL_task();
            List<task> listtk = bll_tk.search(tk, "task_id");
            if (listtk != null)
            {
                if (listtk.Count > 0)
                {
                    listtk[0].task_decript = str_task_decript;
                    listtk[0].task_time = str_task_time;
                    listtk[0].task_type = str_task_type;
                    listtk[0].task_contact = str_task_contact;
                    if (bll_tk.update(listtk[0]))
                    {
                        return true;
                    }
                }
            }
            return false;
        }


        public static bool alter_userinfo(String _user_email, String _user_name, String _user_age, String _user_sex, String _user_qq, String _user_phone, String _user_addr, String _user_ownword)
        {
            _userinfo _us = new _userinfo();


            _us._user_email = _user_email;
            BLL_userinfo bll_us = new BLL_userinfo();
            _us = bll_us.search(_us, "_user_email");

            if(_us != null)
            {
                _us._user_email = _user_email;
                _us._user_name = _user_name;
                _us._user_age = _user_age;
                _us._user_sex = _user_sex;
                _us._user_qq = _user_qq;
                _us._user_phone = _user_phone;
                _us._user_addr = _user_addr;
                _us._user_ownword = _user_ownword;
                _us._user_image = _us._user_image;
                if (bll_us.update(_us))
                {
                    return true;
                }
            }


            return false;
        }

        public static int alter_psw(String _user_email, String _user_oldpsw, String _user_newpsw)
        {
            BLL_user bll_us = new BLL_user();
            _user _us = new _user();
            _us._user_email = _user_email;

            if (bll_us.search(_us) == null)
            {
                return 0;  //没有此用户
            }
            _user new_user = bll_us.search(_us);
            if (!new_user._user_psw.Trim().ToString().Equals(_user_oldpsw))
            {
                return -1;  //旧密码错误 
            }
            _us._user_psw = _user_newpsw;
            if (bll_us.update(_us))
            {
                return 1;
            }
            //_userinfo _us = new _userinfo();
            //_us._user_email = _user_email;
            //_us._user_name = _user_name;
            //_us._user_age = _user_age;
            //_us._user_sex = _user_sex;
            //_us._user_qq = _user_qq;
            //_us._user_phone = _user_phone;
            //_us._user_addr = _user_addr;
            //_us._user_ownword = _user_ownword;
            //BLL_userinfo bll_us = new BLL_userinfo();
            //if (bll_us.update(_us))
            //{
            //    return true;
            //}
            return 2;
        }



        public static string add_contact(String contact_refrences_user_email, String contact_name, String contact_phone, String contact_time)
        {
            contact ct = new contact();
            ct.contact_refrences_user_email = contact_refrences_user_email;
            ct.contact_name = contact_name;
            ct.contact_phone = contact_phone;
            ct.contact_time = contact_time;
            BLL_contact bll_ct = new BLL_contact();
            string contactid = bll_ct.insert(ct);
            if (contactid != null)
            {
                return contactid;
            }
            return null;
        }


        public static bool delete_contact(String contactid)
        {
            contact ct = new contact();

            string str = contactid;
         
            ct.contact_id = Decimal.Parse(str);

            BLL_contact bll_ct = new BLL_contact();
           
            if (bll_ct.delete(ct))
            {
                return true;
            }
            return false;
        }

        public static bool alter_contact(String contactid, String contact_name, String contact_phone)
        {
            contact ct = new contact();

            string str = contactid;

            ct.contact_id = Decimal.Parse(str);
            BLL_contact bll_ct = new BLL_contact();
            ct = bll_ct.search(ct, "contact_id")[0];

            if (ct != null)
            {
                ct.contact_name = contact_name;
                ct.contact_phone = contact_phone;
                if (bll_ct.update(ct))
                {
                    return true;
                }
            }
            return false;
        }


        public static contact get_contact(String contactid)
        {
            contact ct = new contact();

            string str = contactid;

            ct.contact_id = Decimal.Parse(str);
            BLL_contact bll_ct = new BLL_contact();
            ct = bll_ct.search(ct, "contact_id")[0];

            if (ct != null)
            {

                return ct;

            }
            return null;
        }



        public static string add_product(String product_refrences_user_email, String product_name, String product_sum, String product_price, String product_decript)
        {
            product pt = new product();
            pt.product_refrences_user_email = product_refrences_user_email;
            pt.product_name = product_name;
            pt.product_sum = product_sum;
            pt.product_price = product_price;
            pt.product_decript = product_decript;

            BLL_product bll_pt = new BLL_product();
            string productid = bll_pt.insert(pt);
            if (productid != null)
            {
                return productid;
            }
            return null;
        }

        public static bool alter_product(String product_id, String product_name, String product_sum, String product_price, String product_decript)
        {
            product pt = new product();

            string str = product_id;

            pt.product_id = Decimal.Parse(str);
            BLL_product bll_pt = new BLL_product();
            pt = bll_pt.search(pt, "product_id")[0];

            if (pt != null)
            {
                pt.product_name = product_name;
                pt.product_sum = product_sum;
                pt.product_price = product_price;
                pt.product_decript = product_decript;
                if (bll_pt.update(pt))
                {
                    return true;
                }
            }
            return false;
        }


        public static product get_product(String productid)
        {
            product pt = new product();

            string str = productid;

            pt.product_id = Decimal.Parse(str);
            BLL_product bll_pt = new BLL_product();
            pt = bll_pt.search(pt, "product_id")[0];

            if (pt != null)
            {

                return pt;

            }
            return null;
        }


        public static bool delete_product(String product_id)
        {
            product pt = new product();

            string str = product_id;

            pt.product_id = Decimal.Parse(str);

            BLL_product bll_pt = new BLL_product();

            if (bll_pt.delete(pt))
            {
                return true;
            }
            return false;
        }


        public static string add_trade(String trade_customer, String trade_refrences_product_name, String trade_sum, String trade_price, String trade_amount, String trade_decript, String trade_time, String trade_refrences_user_email)
        {
            trade td = new trade();
            td.trade_customer = trade_customer;
            td.trade_refrences_product_name = trade_refrences_product_name;
            td.trade_sum = trade_sum;
            td.trade_price = trade_price;
            td.trade_amount = trade_amount;
            td.trade_decript = trade_decript;
            td.trade_time = trade_time;
            td.trade_refrences_user_email = trade_refrences_user_email;

            BLL_trade bll_td = new BLL_trade();
            string tradeid = bll_td.insert(td);
            if (tradeid != null)
            {
                return tradeid;
            }
            return null;
        }


        public static bool delete_trade(String trade_id)
        {
            trade td = new trade();

            string str = trade_id;

            td.trade_id = Decimal.Parse(str);

            BLL_trade bll_td = new BLL_trade();

            if (bll_td.delete(td))
            {
                return true;
            }
            return false;
        }


        public static trade get_trade(String trade_id)
        {
            trade td = new trade();

            string str = trade_id;

            td.trade_id = Decimal.Parse(str);
            BLL_trade bll_td = new BLL_trade();
            td = bll_td.search(td, "trade_id")[0];

            if (td != null)
            {

                return td;

            }
            return null;
        }

        public static bool alter_trade(String trade_id, String trade_customer, String trade_refrences_product_name,String trade_sum, String trade_price, String trade_amount, String trade_decript, String trade_time)
        {
            trade td = new trade();

            string str = trade_id;

            td.trade_id = Decimal.Parse(str);
            BLL_trade bll_td = new BLL_trade();
            td = bll_td.search(td, "trade_id")[0];

            if (td != null)
            {
                td.trade_customer = trade_customer;
                td.trade_refrences_product_name = trade_refrences_product_name;
                td.trade_sum = trade_sum;
                td.trade_price = trade_price;
                td.trade_amount = trade_amount;
                td.trade_decript = trade_decript;
                td.trade_time = trade_time;
                if (bll_td.update(td))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
